package com.brook;

import com.brook.demo.pojo.Evection;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * 排他网关exclusive gateway
 */
public class ActivitiGatewayExclusive {

    @Test
    public void testDeployment() {
        //获取流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //获取deployService
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        //start deploy
        Deployment deploy = repositoryService.createDeployment()
                .name("出差申请-排他网关")
                .addClasspathResource("bpmn/evection-exclusive.bpmn")
                .deploy();
        //输出
        System.out.println("deploy process id:"+deploy.getId());
        System.out.println("deploy process name:"+deploy.getName());
    }

    @Test
    public void testStartProcess(){
        //获取流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //获取启动service
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        //流程定义id
        String key = "evectionExclusive";

        Map<String, Object> map = new HashMap<>();
        Evection evection = new Evection();
        evection.setNum(4d);
        map.put("evection",evection);

        //start 启动 process
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key, map);

        //输出
        System.out.println("流程实例名称："+processInstance.getName());

    }

    @Test
    public void completeTask(){
        //获取流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //获取task service
        TaskService taskService = defaultProcessEngine.getTaskService();

        //流程定义id
        String key = "evectionExclusive";
        //task 负责人
        //String assignee = "Brook";
        String assignee = "Ada";

        //查询task
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskAssignee(assignee)
                .singleResult();
        //执行完成
        if (task != null) {
            taskService.complete(task.getId());
            System.out.println("task"+task.getName()+"完成");
        }

    }
}
